package com.jwaoo.mobile.view.setting.fragment;

import android.os.Build;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.dx.as.ToolBarOptions;
import com.dx.as.common.FragmentUtils;
import com.dx.as.fragment.TFragment;
import com.jwaoo.mobile.R;
import com.jwaoo.mobile.bean.CommonData;

import java.util.List;
import java.util.Locale;

/**
 * Created by jwaoo on 2017/12/15.
 */

public class FuctionFragment extends TFragment{
    private int type=0;
    private final int language=1,voice=2,video=3;
    @Override
    public void initView() {
        findView(R.id.setting_language).setOnClickListener(onClickListener);
        findView(R.id.setting_voice).setOnClickListener(onClickListener);
        findView(R.id.setting_video).setOnClickListener(onClickListener);
        findView(R.id.setting_clear_cache).setOnClickListener(onClickListener);
        getLanguage();
    }

    @Override
    public int setLayoutI() {
        return R.layout.fragment_setting_function;
    }

    @Override
    public void dataOpion() {

    }

    @Override
    public void initRpo() {

    }

    public void setToolbar(){
        ToolBarOptions toolBarOptions = new ToolBarOptions();
        toolBarOptions.showLeft=true;
        toolBarOptions.titleString = getResources().getString(R.string.function_setting);
        toolBarOptions.onLeftClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentUtils.goBackFragment(getActivity(),SettingFragment.class,R.id.register_container,FuctionFragment.class);
            }
        };
        setToolBar(toolBarOptions);
    }

    private void getLanguage(){
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = getResources().getConfiguration().getLocales().get(0);
        } else {
            locale = getResources().getConfiguration().locale;
        }

        //获取语言的正确姿势：
        String lang = locale.getLanguage() + "-" + locale.getCountry();
        ((TextView)findView(R.id.tv_language)).setText(lang);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.setting_language:
                    type = language;
                    onSeleced();
                    break;
                case R.id.setting_voice:
                    type = voice;
                    break;
                case R.id.setting_video:
                    type = video;
                    onSeleced();
                    break;
                case R.id.setting_clear_cache:
                    type = 0;
                    break;
            }
//            if (type > 0) onSeleced();
        }
    };

    private void onSeleced(){
        List<CommonData.CommonBean> reslut = null;
        switch (type){
            case language:
                reslut = CommonData.getLanguageList();
                break;
            case voice:
                break;
            case video:
                reslut = CommonData.getNetWorkList();
                break;
        }
        final List<CommonData.CommonBean> list = reslut;
        //条件选择器
        final OptionsPickerView pvOptions = new  OptionsPickerView.Builder(getActivity(), new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
                String value = list.get(options1).getValue();
                switch (type){
                    case language:
                        ((TextView)findView(R.id.tv_language)).setText(value);
                        break;
                    case voice:
                        break;
                    case video:
                        ((TextView)findView(R.id.tv_video)).setText(value);
                        break;
                }
            }
        }).build();
        pvOptions.setPicker(list);
        pvOptions.show();
    }
}
